package com.glsc.ngateway.platform.controller.other;

import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.SystemConfig;
import com.glsc.ngateway.common.api.common.dto.PageQueryParamDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhouzp
 * @date 2020/8/19
 * 系统license管理
 */
@RestController
@RequestMapping("/api/systemConfig")
@Api(value = "系统配置", tags = {"系统配置"})
public class SystemConfigController {

    private static Logger logger = LoggerFactory.getLogger(SystemConfigController.class);

    @Resource
    private SystemConfigService systemConfigService;

    @PostMapping("/create")
    @ApiOperation(value = "新增", notes = "新增")
    public PlatformResponse<?> create(@RequestBody SystemConfig systemConfig) {
        systemConfigService.saveOrUpdate(systemConfig);
        return PlatformResponse.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新系统License信息", notes = "更新系统License信息")
    public PlatformResponse<?> update(@RequestBody SystemConfig systemConfig) {
        systemConfigService.saveOrUpdate(systemConfig);
        return PlatformResponse.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除系统License信息", notes = "删除系统License信息")
    public PlatformResponse<?> delete(@ApiParam(value = "id") @RequestBody SystemConfig systemConfig) {
        systemConfigService.deleteById(systemConfig.getConfigId());
        return PlatformResponse.success();
    }

    @GetMapping("/findbyid")
    @ApiOperation(value = "根据ID查询系统授权信息", notes = "根据ID查询系统授权信息")
    public PlatformResponse<SystemConfig> findById(@ApiParam(required = true, value = "系统ID") @RequestParam Integer configId) {
        return PlatformResponse.successData(systemConfigService.findById(configId));
    }

    @GetMapping("/listpage")
    @ApiOperation(value = "查询系统授权信息列表", notes = "查询系统授权信息列表")
    public PlatformResponse<Page<SystemConfig>> listpage(@ApiParam(value = "配置code") @RequestParam(required = false) String configCode,
                                                         @ApiParam(value = "配置名称") @RequestParam(required = false) String configName,
                                                         @ApiParam(value = "配置值") @RequestParam(required = false) String configValue,
                                                         @ApiParam(value = "页码从1开始") @RequestParam(required = false) Integer pageNo,
                                                         @ApiParam(value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {
        SystemConfig param = new SystemConfig();
        param.setConfigCode(configCode);
        param.setConfigName(configName);
        param.setConfigValue(configValue);
        PageQueryParamDto<SystemConfig> pageParam = PageQueryParamDto.<SystemConfig>builder()
                .pageNo(Objects.nonNull(pageNo) ? pageNo : PageConstant.PageNo)
                .pageSize(Objects.nonNull(pageSize) ? pageSize : PageConstant.PageSize)
                .param(param)
                .build();
        return PlatformResponse.successData(systemConfigService.page(pageParam));
    }

    @GetMapping("/findByCode")
    @ApiOperation(value = "查询系统授权信息列表", notes = "查询系统授权信息列表")
    public PlatformResponse<SystemConfig> findByCode(@ApiParam(value = "配置code") @RequestParam(required = false) String configCode) {
        return PlatformResponse.successData(systemConfigService.findByCode(configCode));
    }
}

